home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / Delphi.Net / CLR / Threading / threadinst.dpr < prev    next >
Encoding:
Text File  |  2004-10-22  |  1.3 KB  |  70 lines

  1. program threadinst.dpr;
  2. {$APPTYPE CONSOLE}
  3.  
  4. //
  5. // This example demonstrates how to use native .NET methods to create a thread on 
  6. // a class with an instance method.
  7. //
  8. // Written by: Rick Ross (http://www.rick-ross.com/)
  9. //
  10.  
  11. uses
  12.    System.Threading;
  13.  
  14. type
  15.   ThreadMe = class
  16.   private
  17.     FStartNum : integer;
  18.   public
  19.     procedure MyThreadMethod;
  20.     property StartNum : integer write FStartNum;
  21.   end;
  22.  
  23. procedure ThreadMe.MyThreadMethod;
  24. var
  25.   stop : integer;
  26.   curNum : integer;
  27.  
  28. begin
  29.   curNum := FStartNum;
  30.   stop := FStartNum + 10;
  31.   while curNum < stop do
  32.   begin
  33.     writeln('Thread ', System.Threading.Thread.CurrentThread.Name ,
  34.             ' current value is ',curNum);
  35.     inc(curNum);
  36.     Thread.Sleep( 3 );
  37.   end; 
  38. end;
  39.  
  40. var
  41.   thrdclass1 : ThreadMe;
  42.   thrdclass2 : ThreadMe;
  43.   thrd1 : Thread;
  44.   thrd2 : Thread;
  45.  
  46. begin
  47.   writeln('Staring threading instance example...');
  48.  
  49.   // create myDotNetThread instance
  50.   thrdclass1 := ThreadMe.create;
  51.   thrdclass1.StartNum := 10;
  52.  
  53.   thrd1 := Thread.Create( @thrdclass1.MyThreadMethod );
  54.   thrd1.Name := 'one';
  55.  
  56.   thrdclass2 := ThreadMe.create;
  57.   thrdclass2.StartNum := 100;
  58.  
  59.   thrd2 := Thread.create( @thrdclass2.MyThreadMethod );
  60.   thrd2.Name := 'two';
  61.  
  62.   thrd1.Start();
  63.   thrd2.Start();
  64.  
  65.   readln;
  66.  
  67.   writeln('Done');
  68. end.
  69.  
  70.